<html><head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>Interfaces</title></head>
<body bgcolor="#EFF1F0" link="#3A3966" vlink="#000000" alink="#000000">
<font face="Verdana, sans-serif" size="2"><p align="center"><b><font size="5">Interfaces</font></b></p>

<br><b>语法</b><blockquote><pre><font face="Courier New, Courier, mono"size="2"><b><font color="#3A3966">Interface</font></b> &lt;name&gt; [<b><font color="#3A3966">Extends</font></b> &lt;name&gt;]
  &lt;Method[.&lt;type&gt;]()&gt;
  ...
<b><font color="#3A3966">EndInterface</font></b>
</font></pre></blockquote>
<b>概要</b><br><blockquote>


<b><font color="#3A3966">Interfaces</font></b> are used to access Object Oriented modules, such as COM (Component Object Model) or DirectX dynamic libraries (DLL). 
These types of libraries are becoming more and more common in Windows, and through the use of interfaces, the ability to access these modules 
easily (and without any performance hit) is realized. It also introduces the necessary basis for Object Oriented programming within PureBasic, 
but the use of interfaces requires some advanced knowledge. Most of the standard Windows interfaces have already been implemented within a resident 
file and this allows direct use of these objects. 

<br>
<br>
The optional <b><font color="#3A3966">Extends</font></b> parameter may be used to extend another interface with new functions 
(Theses functions are commonly called 'methods' in Object Oriented (OO) languages such as C++ or Java). 
All functions contained within the extended interface are then made available within the new interface and will be 
placed before the new functions. This is useful in order to do basic inheritance of objects. <br>
<br>
<a href="dim.html">Arrays</a> can be passed as parameters using the <b><font color="#3A3966">Array</font></b> keyword, 
<a href="newlist.html">lists</a> using the <b><font color="#3A3966">List</font></b> keyword and <a href="newmap.html">maps</a> using the <b><font color="#3A3966">Map</font></b> keyword. 
<br>
<br>
A return type may be defined in the interface declaration by adding the type after the method. 
<br>
<br>
<a href="others.html">SizeOf</a> may be used with Interfaces in order to get the size of the interface and <a href="others.html">OffsetOf</a> 
may be used to retrieve the index of the specified function. 
<br>
<br>
The <a href="pseudotypes.html">pseudotypes</a> may be used for the parameters of the functions, but not for the return 
value. 
<br>
<br>
Note: The concept of objects, and the capability provided within PureBasic for their use, has been developed for, and mainly 
targeted towards, experienced programmers. However, an understanding of these concepts and capabilities are in no way a 
prerequisite for creating professional software or games. 

 

</blockquote><p><b>Example:</b> Basic example of object call</p><blockquote>


<pre><font face="Courier New, Courier, mono"size="2">  <font color="#3A3966">; In order to access an external object (within a DLL for example),</font>
  <font color="#3A3966">; the objects' interface must first be declared:</font>

  <b><font color="#3A3966">Interface</font></b> MyObject
<font color="#3A3966">    Move</font>(x,y)
<font color="#3A3966">    MoveF</font>(x.f,y.f)
<font color="#3A3966">    Destroy</font>()
  <b><font color="#3A3966">EndInterface</font></b>
  
  <font color="#3A3966">; CreateObject is the function which creates the object, from the DLL,</font>
  <font color="#3A3966">; whose interface has just been defined.</font>
  <font color="#3A3966">; Create the first object...</font>
  <font color="#3A3966">;</font>
  Object1.MyObject =<font color="#3A3966"> MyCreateObject</font>()
  
  <font color="#3A3966">; And the second one.</font>
  <font color="#3A3966">;</font>
  Object2.MyObject =<font color="#3A3966"> MyCreateObject</font>()
  
  <font color="#3A3966">; Then the functions which have just been defined, may </font>
  <font color="#3A3966">; be used, in order to act upon the desired object.</font>
  <font color="#3A3966">;</font>
  Object1\<font color="#3A3966">Move</font>(10, 20)
  Object1\<font color="#3A3966">Destroy</font>()
  
  Object2\<font color="#3A3966">MoveF</font>(10.5, 20.1)
  Object2\<font color="#3A3966">Destroy</font>()
</font></pre>

</blockquote><p><b>Example:</b> Example with 'Extends'</p><blockquote>


<pre><font face="Courier New, Courier, mono"size="2">  <font color="#3A3966">; Define a basic Cube interface object.</font>
  <font color="#3A3966">;</font>
  <b><font color="#3A3966">Interface</font></b> Cube
<font color="#3A3966">    GetPosition</font>()
<font color="#3A3966">    SetPosition</font>(x)
<font color="#3A3966">    GetWidth</font>()
<font color="#3A3966">    SetWidth</font>(Width)
  <b><font color="#3A3966">EndInterface</font></b>
  
  <b><font color="#3A3966">Interface</font></b> ColoredCube <b><font color="#3A3966">Extends</font></b> Cube
<font color="#3A3966">    GetColor</font>()
<font color="#3A3966">    SetColor</font>(Color)
  <b><font color="#3A3966">EndInterface</font></b>
  
  <b><font color="#3A3966">Interface</font></b> TexturedCube <b><font color="#3A3966">Extends</font></b> Cube
<font color="#3A3966">    GetTexture</font>()
<font color="#3A3966">    SetTexture</font>(TextureID)
  <b><font color="#3A3966">EndInterface</font></b>
  
  <font color="#3A3966">; The interfaces for 3 different objects have now been defined, these objects include:</font>
  <font color="#3A3966">;</font>
  <font color="#3A3966">; - 'Cube' which has the: Get/SetPosition() and Get/SetWidth() functions.</font>
  <font color="#3A3966">; - 'ColoredCube' which has the: Get/SetPosition(), Get/SetWidth() and Get/SetColor() functions.</font>
  <font color="#3A3966">; - 'TexturedCube' which has the: Get/SetPosition(), Get/SetWidth() and Get/SetTexture() functions.</font>
  <font color="#3A3966">;</font>
</font></pre>

</body></html>